package com.xxx.service.impl;

import com.xxx.mapper.ClassInfoMapper;
import com.xxx.pojo.ClassInfo;
import com.xxx.service.Services;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class ClassInfoServiceImpl implements Services<ClassInfo> {



    SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();


    @Override
    public ClassInfo selectByUsername(String username) {
        return null;
    }

    @Override
    public List<ClassInfo> selectAll(Integer index, Integer num) {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        List<ClassInfo> classInfos = mapper.selectAll(index, num);
        sqlSession.close();
        return classInfos;
    }

    @Override
    public int add(ClassInfo classInfo) {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        int i = mapper.addClassInfo(classInfo);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int delete(Long id) {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        int i = mapper.delByOrder(id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public ClassInfo selectById(Long id) {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        ClassInfo classInfo = mapper.selectByOrder(id);
        return  classInfo;
    }

    @Override
    public int updateById(ClassInfo classInfo) {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        int i = mapper.updateClassInfo(classInfo);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int selectCount() {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        int i = mapper.selectCount();
        return i;
    }
}
